﻿using Shell.Library.Helpers;
using Shell.Library.Models.Interface;
using Shell.Library.Services;
using System;
using Catel;
using Catel.Logging;
using Catel.MVVM;
using Shell.Views;

namespace Shell.Services
{
     public class ShellService : ServiceBase, IShellService
    {
        private static readonly ILog Log = LogManager.GetCurrentClassLogger();

        private readonly MainWindow _shell;

        public ShellService(MainWindow shell)
        {
            Argument.IsNotNull("shell", shell);

            _shell = shell;
        }

        public bool ShowDebuggingWindow
        {
            get { return _shell.IsAnchorableVisible(MainWindow.TraceOutputAnchorable); }
            set
            {
                if (value)
                {
                    _shell.ShowAnchorable(MainWindow.TraceOutputAnchorable);
                }
                else
                {
                    _shell.HideAnchorable(MainWindow.TraceOutputAnchorable);
                }
            }
        }

        public void ShowDocument<TViewModel>(object tag = null) where TViewModel : IViewModel, new()
        {
            var viewModel = new TViewModel();

            ShowDocument(viewModel, tag);
        }

        public void ShowDocument<TViewModel>(TViewModel viewModel, object tag = null) where TViewModel : IViewModel
        {
            Argument.IsNotNull("viewModel", viewModel);

            Log.Debug("Showing document for view model '{0}'", viewModel.UniqueIdentifier);

            var viewLocator = GetService<IViewLocator>();
            var viewType = viewLocator.ResolveView(viewModel.GetType());

            var document = AvalonDockHelper.FindDocument(viewType, tag);
            if (document == null)
            {
                var view = ViewHelper.ConstructViewWithViewModel(viewType, viewModel);
                document = AvalonDockHelper.CreateDocument(view, tag);
            }

            AvalonDockHelper.ActivateDocument(document);

            Log.Debug("Showed document for view model '{0}'", viewModel.UniqueIdentifier);
        }

        public void ShowToolBoxItem<TViewModel>(object tag = null) where TViewModel : IViewModel, new()
        {
            var viewModel = new TViewModel();

            ShowToolBoxItem(viewModel, tag);
        }

        public void ShowToolBoxItem<TViewModel>(TViewModel viewModel, object tag = null) where TViewModel : IViewModel
        {
            Argument.IsNotNull("viewModel", viewModel);

            Log.Debug("Showing document for view model '{0}'", viewModel.UniqueIdentifier);

            var viewLocator = GetService<IViewLocator>();
            var viewType = viewLocator.ResolveView(viewModel.GetType());

            var document = AvalonDockHelper.FindToolBoxItem(viewType, tag);
            if (document == null)
            {
                var view = ViewHelper.ConstructViewWithViewModel(viewType, viewModel);
                document = AvalonDockHelper.CreateToolBoxItem(view, tag);
            }

            AvalonDockHelper.ActivateDocument(document);

            Log.Debug("Showed document for view model '{0}'", viewModel.UniqueIdentifier);
        }

        
        public void AddRibbonItem(IRibbonItem ribbonItem)
        {
            // Marked obsolete on the interface
            throw new NotImplementedException();
        }

        public void RemoveRibbonItem(IRibbonItem ribbonItem)
        {
            // Marked obsolete on the interface
            throw new NotImplementedException();
        }
    }
}